
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;

import logger.PoolLogger;

import dbmanager.*;
import dbmanager.GetIdName;

public class CombineDatabase 
{
	DBOperation dbClient;
    GetIdName gid;
    PoolLogger pl;
    
    public CombineDatabase() {
    	// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
    
    public void setClient(String _dsn)
    {
    	dbClient = new DBOperation(_dsn);
    	dbClient.getConnect();
    }
   /* public void SetDBManager(DBManager db)
    {
    	this.db = db;
    	gid =new GetIdName(db); 
    }*/
    
    public void combineDatabase()
    {
    	try 
    	{
			ResultSet rs = dbClient.getSelect("Select * from property_details");
			while(rs.next())
			{
				String mid=rs.getString("mid");
				String pid=rs.getString("pid");
				String pv=rs.getString("pv");
				String vt=rs.getString("vt");
				String ref=rs.getString("ref");
				String midtext = getMasterName(mid, dbClient);
				String pidtext = getMasterName(pid, dbClient);
				String pvtext="0";
				if(!pv.equals("0"))
					pvtext = getMasterName(pv, dbClient);
				String reftext = getMasterName(ref, dbClient);
				
				String newmid =  gid.getId(midtext);
				if(newmid.equals("0"))
				{
					newmid= insertInMaster(midtext);
				}
				String newpid =  gid.getId(pidtext);
				if(newpid.equals("0"))
				{
					newpid= insertInMaster(pidtext);
				}
				String newvt=vt;
				String newpv =  "0";
				if(!pv.equals("0"))
				{
					newpv =  gid.getId(pvtext);
									
					if(newpv.equals("0"))
					{
						newpv= insertInMaster(pvtext);
						newvt="no$";
					}
				}
				String newref =  gid.getId(reftext);
				if(newref.equals("0"))
				{
					newref= insertInMaster(reftext);
				}
				Hashtable<Object, Object> table = new Hashtable<Object, Object>();
				table.put("mid", newmid);
				table.put("pid",newpid);
				table.put("pv",newpv);
				table.put("vt",newvt);
				table.put("ref",ref);
				DBManager.getInsert("property_details", table);
			}
		}
    	catch (Exception e) 
    	{
			e.printStackTrace();
		}
    	
    }
    
    public String insertInMaster(String midtext)
    {
    	ResultSet rs= DBManager.getSelect("Select id from max_id_table where table_name='master_table'");
    	String id ="";
    	try 
    	{	
			if(rs.next())
			{
				Hashtable<Object, Object> table = new Hashtable<Object, Object>();
				id= rs.getString("id");
				int nextvalue= Integer.parseInt(id) +1;
	    		table.put("name", midtext);
	    		table.put("id", nextvalue);
	    		DBManager.getInsert("master_table", table);
	    		DBManager.getUpdate("update max_id_table set id='"+nextvalue+"' where table_name='master_table'");
				return Integer.toString(nextvalue);
			}
		} 
    	catch (Exception e) 
    	{
			e.printStackTrace();
		}
		return id;
    }
    
    public String getMasterName(String id,DBOperation db)
    {
    	ResultSet rs =DBManager.getSelect("Select name from master_table where id='"+id+"'");
    	try {
			if(rs.next())
			{
				String name = rs.getString("name");
				return name;
			}
			else
				return "no$";
		} catch (Exception e) 
		{
			return "no$";
		}
    }
    public String getMasterid(String name)
    {
    	ResultSet rs =DBManager.getSelect("Select name from master_table where name='"+name+"'");
    	try {
			if(rs.next())
			{
				String id = rs.getString("id");
				return id;
			}
			else
				return "0";
		} catch (Exception e) 
		{
			return "0";
		}
    }
    public static void main(String[] args) 
    {
    	CombineDatabase cd =new CombineDatabase();

    	cd.setClient("//192.168.1.130/gui");
    	cd.combineDatabase();
	}
}
